package com.acme.faturamento.model.businessdelegate;

import java.util.Collection;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


@Stateless
public class PersistenceManager implements PersistenceManagerLocal,PersistenceManagerRemote{

    @PersistenceContext
    private EntityManager manager;

    public static final String RemoteJNDIName =  PersistenceManager.class.getSimpleName() + "/remote";

    public static final String LocalJNDIName =  PersistenceManager.class.getSimpleName() + "/local";

    public void salvar(Object objeto){
        manager.persist(objeto);
    }
    public <T> void apagar(Class<T> classe,Long pk){
        manager.remove(manager.find(classe,pk));
    }
    public void atualizar(Object objeto){
        manager.refresh(objeto);
    }
    public void mesclar(Object objeto){
        manager.merge(objeto);
    }
    public <T> T get(Class<T> entidadeClass, Object chavePrimaria){
        return manager.find(entidadeClass,chavePrimaria);
    }

    @SuppressWarnings("unchecked")
    public <T> Collection<T> getLista(String nomeQuery){
        List resultado = manager.createNamedQuery(nomeQuery).getResultList();
        return resultado;
    }

}
